﻿Imports Infragistics.Win

Public Class frmDeleteFund

#Region "Declaration"
    Private stopresize As Boolean = False
    Public Event deletefund(ByVal dt As DataTable, ByVal isDeleted As Boolean)
    Private _datatable As DataTable
    Delegate Sub deleterecords()
    Dim updatedTable As DataTable = Nothing
    Dim _strTemp As DataTable = Nothing
    Dim deletedTable As DataTable = Nothing
#End Region

#Region "Properties"
    Public Property Datatable() As DataTable
        Get
            Return _datatable
        End Get
        Set(ByVal value As DataTable)
            _datatable = value
        End Set
    End Property
#End Region

#Region "Button Events"
    Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Try
            Me.Close()
        Catch ex As Exception
            If (Not Me.IsDisposed) Then
                MessageBox.Show(ex.Message)
            End If
        End Try
    End Sub

    Private Sub btnOk_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim _FundId As String
        Dim CAddModifyInvestor As New cAddModifyFund
        Dim _strFundName As String = ""
        Dim _strMessage As String = ""
        Dim d As deleterecords = Nothing
        Try
            If btnCancel.Enabled = False Then
                If MessageManager.GetMessage("FUNDMARKEDASDLT") = lblmessage.Text Then
                    Exit Sub
                End If
            End If
            lblInform.Visible = False
            btnCancel.Enabled = False
            updatedTable = CType(grdFunds.DataSource, DataTable).Clone
            _strTemp = grdFunds.DataSource
            deletedTable = CType(grdFunds.DataSource, DataTable).Clone
            For i As Integer = 0 To grdFunds.Rows.Count - 1
                _FundId = grdFunds.Rows(i).Cells("fundID").Value.ToString()
                _strMessage = cAddModifyFund.Delete_Funds(_FundId)
                If (_strMessage <> "Fund deleted successfully") Then
                    If _strMessage = "Fund updated successfully" Then
                        updatedTable.ImportRow(_strTemp.Rows(i))
                    End If
                Else
                    deletedTable.ImportRow(_strTemp.Rows(i))
                End If
            Next
            d = New deleterecords(AddressOf delete)
            Me.BeginInvoke(d)
        Catch ex As Exception
            Throw ex
            stopresize = True
        Finally
            d = Nothing
            If MessageManager.GetMessage("FUNDMARKEDASDLT") = lblmessage.Text Then
                Me.Close()
            End If
        End Try
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.Close()
    End Sub
#End Region

#Region "Methods"
    Public Sub New(ByVal dt As DataTable)
        InitializeComponent()
        Datatable = dt
    End Sub

    Private Sub ShowMessageInfo(ByVal message As String)
        MessageBox.Show(message, "Crystal", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub delete()
        Try
            If (updatedTable.Rows.Count > 0) Then
                'grdFunds.DataSource = Nothing
                Application.DoEvents()
                grdFunds.DataSource = updatedTable
                RaiseEvent deletefund(updatedTable, False)
                HideGridColumns()
                lblmessage.Text = MessageManager.GetMessage("FUNDMARKEDASDLT")
            End If
            If (deletedTable.Rows.Count > 0) Then
                Me.Focus()
                RaiseEvent deletefund(deletedTable, True)
                ShowMessageInfo(MessageManager.GetMessage("FUNDSDELETED"))

            End If
            If (updatedTable.Rows.Count = 0) Then
                Me.Close()
            End If
        Catch ex As Exception
            stopresize = True
            MessageBox.Show(ex.Message)
        Finally
            If (Not Me.IsDisposed) Then
                Me.Focus()
            End If
            If (deletedTable.Rows.Count > 0) Then
                stopresize = True
            End If
        End Try
    End Sub

    Public Sub HideGridColumns()
        Try
            Dim column As UltraWinGrid.UltraGridColumn
            Dim listOfHiddenColumns As New List(Of UltraWinGrid.UltraGridColumn)
            For Each column In grdFunds.DisplayLayout.Bands(0).Columns
                If column.Index < 1 Or column.Index > 5 Then
                    column.Hidden = True
                End If
            Next
        Catch ex As Exception
            'Throw ex
        End Try
    End Sub

#End Region

#Region "Form Events"
    Private Sub frmDeleteFund_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "FundManagement" Then
                RemoveHandler deletefund, AddressOf CType(Application.OpenForms(lmFomrs), FundManagement).raiseeventfordelete
            End If
        Next
        Me.Dispose()
    End Sub

    Private Sub frmDeleteFund_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        grdFunds.DataSource = Datatable
        If grdFunds.Rows.Count > 0 Then
            grdFunds.Rows(0).Activation = Activation.ActivateOnly
        End If
        HideGridColumns()
        FormatGrid(grdFunds)
        lblmessage.Text = String.Empty
        Me.grdFunds.DisplayLayout.Override.CellClickAction = Infragistics.Win.UltraWinGrid.CellClickAction.CellSelect
    End Sub

    Private Sub frmDeleteFund_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize

        For lmFomrs As Integer = 0 To Application.OpenForms.Count - 1
            If Application.OpenForms(lmFomrs).Name = "frmDeleteFund" Then
                If (Me.WindowState = FormWindowState.Normal) Then
                    CType(Application.OpenForms(lmFomrs), frmDeleteFund).WindowState = Me.WindowState
                End If
                If (Me.WindowState = FormWindowState.Maximized) Then
                    CType(Application.OpenForms(lmFomrs), frmDeleteFund).WindowState = Me.WindowState
                End If
                If (Me.WindowState = FormWindowState.Minimized) Then
                    If (stopresize = True) Then Exit Sub
                    Me.WindowState = FormWindowState.Normal
                    Application.DoEvents()
                End If
            End If
        Next
    End Sub
#End Region

End Class